# -*- coding: utf-8 -*-
# -*- frozen_string_literal: true -*-
require "rbconfig"

module Rep1y
  Port = 8080
  Dir = "public"
end

task :default => :start

desc "Start rep1y"
task :start => [:open, :serve]

desc "Copy reply"
task :copy do
  system(*%W[cp -R ./index.html ./scripts ./style #{ENV.fetch("REP1Y_DIR", Rep1y::Dir)}])
end

# private (unlisted, yet available)
task :serve do
  sh(*%W[ruby -run -e httpd . -p #{ENV.fetch("REP1Y_PORT", Rep1y::Port)}])
end

# private (unlisted, yet available)
task :open do
  rep1y = "http://localhost:#{ENV.fetch("REP1Y_PORT", Rep1y::Port)}"
  # Firefox CLI won't work on macOS https://bugzilla.mozilla.org/show_bug.cgi?id=393645#c10
  open_rep1y =
    if RbConfig::CONFIG["host_os"].match? "darwin"
      ff = FileList["/Applications/Firefox*.app/Contents/MacOS/firefox"].first
      %W[open -a #{ff} #{rep1y}]
    else
      %W[firefox -new-tab #{rep1y}]
    end

  system(*open_rep1y)
end
